/**
 * Created by lifeng on 2018/10/10.
 */
console.log("background.js 开始运行在后台");
//可以调用绝大多数谷歌api
//它随着浏览器的打开而打开
localStorage["realUrl"] = "http://192.168.31.12:8128/cloud-cashier/xml";
//接受注入的js发送过来的请求
chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        //request 是请求参数
        alert("请求数据:"+request.kw);
        //sender 请求源的 url，tab，插件id等信息 浏览器debug可以看具体信息
        console.log("请求来源url:"+sender.url);

        $.ajax({
            url: localStorage['realUrl'],
            type:'post',
            data: {"merid":"111111111","transdata":"11111111111111111111111"},
            success: function(data) {
                sendResponse(data);
            },
            complete: function() {
            },
            error: function(res) {
                sendResponse({"code":1,"msg":"采集失败，请重试。","res":res})
            }
        });
        //设置响应消息
        sendResponse({"msg":"收到请求数据，处理成功"});
    }
);

function myback(){
    alert("我是background.js中的方法");
    // background.js 执行或获取popup.html/js 中的代码
    var views = chrome.extension.getViews({type:'popup'});
    if(views.length > 0) {
        console.log(views[0].location.href);
        //执行注入的js
        myview();
    }
}
//创建右键菜单
chrome.contextMenus.create({
    title: "测试右键菜单",
    onclick: function(){alert('您点击了右键菜单！');}
});

//监听所有请求
chrome.webRequest.onBeforeRequest.addListener(function(details){
        //此处可以拦截所以请有请求
        console.log("修改前的请求地址" + details.url);
        var url=details.url;
        url=url.replace("http", "https");
        details.url=url;
        console.log("修改后的请求地址" + details.url);
        //return {cancel: true};//cancel表示取消本次请求
        return true;//cancel表示取消本次请求
    },
    {urls: ["<all_urls>"]},
    ["blocking"]
);
